package cn.glenn._03vehicle.controller;

import cn.glenn._03vehicle.base.response.JsonResult;
import cn.glenn._03vehicle.mapper.VehicleMapper;
import cn.glenn._03vehicle.pojo.dto.VehicleAddParam;
import cn.glenn._03vehicle.pojo.dto.VehicleListQuery;
import cn.glenn._03vehicle.pojo.enity.Vehicle;
import cn.glenn._03vehicle.pojo.vo.UserVO;
import cn.glenn._03vehicle.pojo.vo.VehicleVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.Size;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("v1/vehicle/")
@Slf4j
@Tag(name = "02-车辆管理模块", description = "02-车辆管理模块",extensions ={
        @Extension(properties = {@ExtensionProperty(name = "x-order", value = "2", parseValue = true)}
        )})
@Validated
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @PostMapping("add")
    @Parameters({
            @Parameter(name="brand",description = "品牌名称",required = true),
            @Parameter(name="license",description = "车辆牌照",required = true),
            @Parameter(name="model",description = "车型名称",required = true),
            @Parameter(name="code",description = "车辆识别代码",required = true),
            @Parameter(name="displacement",description = "车辆排量",required = true),
            @Parameter(name="type",description = "车辆类型",required = true),
            @Parameter(name="color",description = "车辆颜色",required = true),
            @Parameter(name="batteryType",description = "电池类型",required = true),
    })
    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 1)
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session) {
        log.debug("vehicleAddParam:{}", vehicleAddParam);
        Vehicle vehicle = new Vehicle();
        UserVO user = (UserVO) session.getAttribute("user");
        Long userId = (long) user.getId();
        log.debug("userId:{}", userId);
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setUserId(userId);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        int result = vehicleMapper.insertVehicle(vehicle);
        if (result==1) return JsonResult.success(vehicle);
        else return JsonResult.fail();
    }

    @GetMapping("list")
    @Parameters({
            @Parameter(name="brand",description = "品牌名称"),
            @Parameter(name="license",description = "车辆牌照")
    })
    @Operation(summary = "车辆列表")
    @ApiOperationSupport(order = 2)
    public JsonResult listVehicle(VehicleListQuery listQuery,HttpSession session) {
        UserVO user = (UserVO) session.getAttribute("user");
        listQuery.setUserId((long)user.getId());
        List<VehicleVO> vehicleVOS = vehicleMapper.vehicleList(listQuery);
        return JsonResult.success(vehicleVOS);
    }

    @PostMapping("del/{id}")
    @Parameter(name = "id",description = "车辆ID")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 3)
    public JsonResult deleteVehicle(@Range(min = 1,max = 500,message = "车辆编号必须在1-500之间") @PathVariable Integer id, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        int result = vehicleMapper.deleteVehicle(id,userVO.getId());
        if (result==1) return JsonResult.success();
        else return JsonResult.fail();
    }



}
